package com.example.loancalculator;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
 
public class LoanCalculatorActivity extends Activity {
    private EditText mLoanAmount, mInterestRate, mLoanPeriod;
    private TextView mMontlyPaymentResult, mTotalPaymentsResult;
     
    /** Initializes the app when it is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_calculator);
        mLoanAmount = (EditText)findViewById(R.id.date_of_order);
        mInterestRate = (EditText)findViewById(R.id.offer);
        mLoanPeriod = (EditText)findViewById(R.id.appromximate_costs);
        mMontlyPaymentResult = (TextView)findViewById(R.id.date_of_payment_result); 
        mTotalPaymentsResult = (TextView)findViewById(R.id.amount_of_payment_result);
    }
     
    public void showLoanPayments(View clickedButton) {
         
        double loanAmount = Integer.parseInt(mLoanAmount.getText().toString());
        double interestRate = (Integer.parseInt(mInterestRate.getText().toString()));
        double loanPeriod = Integer.parseInt(mLoanPeriod.getText().toString());
        double r = interestRate/1200;
        double r1 =  Math.pow(r+1,loanPeriod);
         
        double monthlyPayment = (double) ((r+(r/(r1-1))) * loanAmount);
        double totalPayment = monthlyPayment * loanPeriod;
         
        mMontlyPaymentResult.setText(new DecimalFormat("##.##").format(monthlyPayment));
        mTotalPaymentsResult.setText(new DecimalFormat("##.##").format(totalPayment));
           }
}